﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace B2B.Extensions
{
    public class ExComboItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
    public class ByteToImageConverter : IValueConverter
    {
        public BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray)
        {
            BitmapImage img = new BitmapImage();
            using (MemoryStream memStream = new MemoryStream(imageByteArray))
            {
                img.SetSource(memStream);
            }
            return img;
        }


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BitmapImage img = new BitmapImage();
            if (value != null)
            {
                img = this.ConvertByteArrayToBitMapImage(value as byte[]);
            }
            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    public class DateConverter : IValueConverter
    {

        #region IValueConverter Members

        /// <summary>
        /// Converts values on their way to the UI for display
        /// </summary>
        /// <param name="value">The value to be formatted</param>
        /// <param name="targetType">The target type of the conversion</param>
        /// <param name="parameter">A format string to be used in the 
        /// formatting of the value</param>
        /// <param name="culture">The culture to use for formatting</param>
        /// <returns>The converted or formatted object</returns>
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatterString = parameter.ToString();
                if (!string.IsNullOrEmpty(formatterString))
                {
                    return string.Format(culture, formatterString, value);
                }
            }
            return value.ToString();
        }

        /// <summary>
        /// Converts values on their way back from the UI to the backend.
        /// </summary>
        /// <param name="value">The value to be formatted</param>
        /// <param name="targetType">The target type of the conversion</param>
        /// <param name="parameter">A format string to be used in the 
        /// formatting of the value</param>
        /// <param name="culture">The culture to use for formatting</param>
        /// <returns>The converted or formatted object</returns>
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToDateTime(value, CultureInfo.CurrentUICulture);
        }

        #endregion
    }
}
